package com.breadenglish.cms.api.app.sys.servlet;

import com.breadenglish.cms.api.app.common.utils.JacksonObjectMapper;
import com.breadenglish.cms.api.app.common.utils.PropertyConfigurerUtils;
import com.breadenglish.cms.api.app.dao.FileDao;
import com.breadenglish.cms.api.app.domain.constants.Constants;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者: guo yao
 * 日期: 2015年12月31日
 * 时间: 下午10:31
 */
public class UploadServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            MiniUploader up = new MiniUploader(request);
//            Uploader up = new Uploader(request);
            String filePath = PropertyConfigurerUtils.getProperty(Constants.FILE_PATH);
            if (filePath.equals("/upload")) {
                filePath = request.getRealPath(filePath);
            }
            up.setSavePath(filePath);
//            up.setSavePath("d:/export/Data/file");
//            up.upload();
//            up.fileUpload();
//
            String[] fileType = {".gif", ".png", ".jpg", ".jpeg", ".bmp"};
            up.setAllowFiles(fileType);
            up.setMaxSize(10000); //单位KB
//            up.fileUpload();
            up.upload();
            String callback = request.getParameter("callback");
//
            Map<String, Object> resultMap = new HashMap<String, Object>();
            resultMap.put("name", up.getFileName());
            resultMap.put("originalName", up.getOriginalName());
            resultMap.put("size", up.getSize());
            resultMap.put("state", up.getState());
            resultMap.put("type", up.getType());
            resultMap.put("url", up.getUrl());
//            String result = JacksonObjectMapper.getInstance().writeValueAsString(resultMap);
            String result = "{\"name\":\"" + up.getFileName() + "\", \"originalName\": \"" + up.getOriginalName() + "\", \"size\": " + up.getSize() + ", \"state\": \"" + up.getState() + "\", \"type\": \"" + up.getType() + "\", \"url\": \"" + up.getUrl() + "\"}";
            result = result.replaceAll("\\\\", "\\\\");

            // 保存文件表
            ServletContext servletContext = this.getServletContext();
            WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
            FileDao fileDao = (FileDao) ctx.getBean("fileDao");
            com.breadenglish.cms.api.app.domain.db.File file = new com.breadenglish.cms.api.app.domain.db.File();
            Long fileId = fileDao.getFileId();
            file.setFileId(fileId);
            file.setFileType(Constants.FILE_TYPE_DEFAULT);
            file.setFileClass(Constants.FILE_CLASS_DEFAULT);
            file.setState(Constants.FILE_CLASS_STATE_TEMP);
            file.setFileUrl(up.getUrl());
            file.setFileName(up.getFileName());
            file.setSize(Double.valueOf(up.getSize()));
            file.setExt(up.getType());
            file.setDelTag(Constants.NOT);
            file.setCreateTime(new Date());
            fileDao.insert(file);


            if (callback == null) {
                response.getWriter().print(result);
            } else {
                response.getWriter().print("<script>" + callback + "(" + result + ")</script>");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
